﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CSharpToJavascript {

	public static class ElementFinder {
		private static Type[] types = null;
		private static Object typesLock = new Object();

		public static Type[] Get() {
			if (types == null) {
				lock (typesLock) {
					if (types == null) {
						List<Type> tempTypes = new List<Type>();
						foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
							try {
								foreach (Type t in a.GetTypes()) {
									Object[] attrs = t.GetCustomAttributes(typeof(ToJavascriptAttribute), true);
									if (attrs.Length > 0) {
										tempTypes.Add(t);
									}
								}
							}
							catch { }
						}
						types = tempTypes.ToArray();
					}
				}
			}

			return types;
		}
	}
}